package org.yangspring.samples.mvc.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.yangspring.samples.mvc.domain.WhyangBank;

import java.util.List;

import static java.lang.System.out;

/**
 * Created by whyang on 2017/06/20.
 */

@Repository
public class WhyangbankDAOImpl implements WhyangBankDAO {

    private static final Logger logger = LoggerFactory.getLogger(BooksDaoImpl.class);

    @Autowired
    @Qualifier("hibernate5AnnotatedSessionFactoryPGsql")
    private SessionFactory sessionFactory;

    /*@Autowired
    public void setSessionFactory(@Qualifier(value = "hibernate5AnnotatedSessionFactoryPGsql") SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }*/

    public List<WhyangBank> listAccountOLD(){

        logger.info("\nBankDAO list\n");

        if (this.sessionFactory == null){
            logger.info("\n\nthis.sessionFactory is NULL\n\n");
        }
        Session session = this.sessionFactory.getCurrentSession();
        List<WhyangBank> accountList = session.createQuery("from WhyangBank").list();
        for(WhyangBank b : accountList){
            logger.info("Account List::" + b);
        }
        return accountList;
//        return new ArrayList<Company>();
    }


    public List<WhyangBank> listAccount(){

        logger.info("\n[new new new] BankDAO list\n");

//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        List<WhyangBank> accountList = session.createQuery("from WhyangBank").list();
        for(WhyangBank b : accountList){
            logger.info("Account List::" + b);
        }
        return accountList;
    }

}
